#include "BackGroundPerPixelDepth.h"


BackGroundPerPixelDepth::
        BackGroundPerPixelDepth( double containment , uint16_t noReading ,
                                 int imgWidth , int imgHeight , int numFrameBG )
                                     : BackGroundPerPixel<uint16_t>(containment,3,2,imgWidth,imgHeight,1,numFrameBG)
{
    this->noReading = noReading;
}

void BackGroundPerPixelDepth::findMovement( const cv::Mat &image , cv::Mat &movement )
{
    const uint16_t *srcRowPtr = (const uint16_t *)image.datastart;
    uint8_t *dstRowPtr = (uint8_t *)movement.datastart;
    const uint16_t *medianRowPtr = (const uint16_t *)median.datastart;

    int index = 0;

    for( int i = 0; i < imgHeight; i++ ) {
        const uint16_t *srcPtr = srcRowPtr;
        uint8_t *dstPtr = dstRowPtr;
        const uint16_t *medianPtr = medianRowPtr;

        for( int j = 0; j < imgWidth; j++ , index++ ) {
            uint16_t p = *srcPtr++;

            if( abs( (int)p - (int)*medianPtr++ ) > tol[index] && p < noReading) {
                *dstPtr++ = 255;
            } else {
                *dstPtr++ = 0;
            }
        }
        srcRowPtr += image.step/depth;
        dstRowPtr += movement.step;
        medianRowPtr += median.step/depth;
    }
}
